let express = require('express')
let app = express()
let db = require('./DB')
let cors = require('cors')
app.use(cors())



// let sql = 'SELECT * FROM kss'
// db.query(sql, (err, result) => {
//     console.log(err, result);
// })

// 详情
app.get('/xqing', function (req, res) {
    let sql = "SELECT * FROM kss"
    db.query(sql, (err, result) => {
        // console.log(err);
        if (err) {
            res.json({
                code: -1,
                msg: "查询失败",
            })
        } else {
            res.json({
                code: 200,
                msg: "查询成功",
                data: result,
            })
        }
    })
})




app.post('/create', (req, res) => {
    // [req.body.username, req.body.money, req.body.phone],
    // console.log(111);
    let sql = `INSERT INTO user(username, money, phone) VALUES (?,?,?)`
    // console.log(req.query.username);
    // console.log(req.query.money)
    // console.log(req.query.phone)


    db.query(sql,
        [req.query.username, req.query.money, req.query.phone],
        (err, result) => {
            if (err) {
                res.json({
                    code: -1,
                    msg: '添加失败',
                })
            } else {
                res.json({
                    code: 200,
                    msg: '添加成功',
                    // err: result,
                })
            }
        })
})



// 删除
app.get("/del", (req, res) => {
    let sql = `DELETE FROM kss WHERE house_id = ${req.query.house_id}`
    db.query(sql, (err, result) => {
        if (err) {
            res.json({
                code: 1,
                msg: "删除失败",
            });
        } else {
            res.json({
                code: 0,
                msg: "删除成功",
            }); 
        }
    });
});

app.listen(3000, () => {
    console.log('http://127.0.0.1:3000')
})